/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.badoo.mobileDemo;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.window.dialog.ToastDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import com.badoo.mobile.util.WeakHandler;

/**
 * 首页
 *
 * @since 2021-04-07
 */
public class MainAbility2 extends Ability {
    private Button textView;
    private Button text2;
    private final int delay = 20000;

    private EventHandler handler = new EventHandler(EventRunner.getMainEventRunner());
    private WeakHandler handler1 = new WeakHandler(EventRunner.getMainEventRunner());

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main2);

        textView = (Button) findComponentById(ResourceTable.Id_text1);
        textView.setClickedListener(component -> {
            showToast("发送task ,延迟20秒执行");

            handler.postTask(() -> {
                showToast("系统发送Task执行结束");
            }, delay);
        });
        text2 = (Button) findComponentById(ResourceTable.Id_text2);
        text2.setClickedListener(component -> {
            showToast("第三方 发送task,延迟20秒执行");

            handler1.postTask(new Runnable() {
                @Override
                public void run() {
                    showToast("第三方Task执行结束");
                }
            }, delay);
        });
    }

    private void showToast(String txt) {
        new ToastDialog(getApplicationContext())
            .setText(txt)
            .show();
    }
}
